<include file="Public/header"/>
<include file="Public/menu"/>

<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            首页
            <small>采购</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('Qgs/index/index')}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="{:U('vendor/index')}"><i class="fa fa-dashboard"></i> 采购</a></li>
            <li class="active">店铺可采购商品配置</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">

        <div class="tab-content">
            <div class="tab-pane active" id="tab_0">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title">{$local_name}</h3>
                        <div class="form-inline pull-right">
                            <form action="{:U('purchase/index',array('local_id'=>$local_id,'order_field'=>'product_id','order'=>$order), '')}" method="get" pjax-container="">
                                <fieldset>
                                    <select class="form-control" name="search_field" style="height: 30px; font-size: 12px;">
                                        <option value="name" <if condition="$search_field eq 'name'">selected="selected"</if>>货品名称</option>
                                        <option value="barcode" <if condition="$search_field eq 'barcode'">selected="selected"</if>>条码</option>
                                        <option value="bn" <if condition="$search_field eq 'bn'">selected="selected"</if>>BN</option>
                                    </select>
                                    <div class="input-group input-group-sm">
                                        <div class="input-group input-group-sm">
                                            <input type="text" class="form-control" placeholder="" name="search" value="{$search}">
                                        </div>
                                    </div>

                                    <div class="input-group input-group-sm">
                                        <div class="input-group-btn">
                                            <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>
                                            </button>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
                        </div>

                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover table-view-list">
                            <tbody>
                            <tr>
                                <th><input type="checkbox" class="grid-select-all"></th>
                                <th>货品ID<a class="fa fa-fw fa-sort" href="{:U('purchase/index',array('p'=>$p,'order_field'=>'product_id','order'=>$order), '')}"></a></th>
                                <th>商品ID<a class="fa fa-fw fa-sort" href="{:U('purchase/index',array('p'=>$p,'order_field'=>'goods_id','order'=>$order), '')}"></a></th>
                                <th>名称</th>
                                <th>图片</th>
                                <th>条码<a class="fa fa-fw fa-sort" href="{:U('purchase/index',array('p'=>$p,'order_field'=>'barcode','order'=>$order), '')}"></a></th>
                                <th>BN<a class="fa fa-fw fa-sort" href="{:U('purchase/index',array('p'=>$p,'order_field'=>'bn','order'=>$order), '')}"></a></th>
                                <th>采购价格</th>
                                <th>最少采购个数</th>
                                <th>购买数据</th>
                                <th>操作</th>
                            </tr>
                            <volist name="list" id="vo">
                                <tr class="cg-product-tr validation-area">
                                    <td><input type="checkbox" class="grid-item" data-id="{$vo.product_id}" ></td>
                                    <td>{$vo.product_id}</td>
                                    <td>{$vo.goods_id}</td>
                                    <td><a href="javascript:;" class="short-gname" title="{$vo.name}">{$vo.name}</a></td>
                                    <td><img src="{$vo.goods_id|getThumb=###}" data-bimg="{$vo.goods_id|getThumb=###}" alt="" class="scale-img"></td>
                                    <td>{$vo.barcode}</td>
                                    <td>{$vo.bn}</td>
                                    <td>{$vo.price|default=$vo['guidance_price']}</td>
                                    <td>{$vo.store|default=$defaultnum}</td>
                                    <td>
                                        <input type="text" data-min="{$vo.store|default=$defaultnum}" class="form-control grid-num" name="num" value="{$vo.store|default=$defaultnum}">
                                    </td>
                                    <td>
                                        <a class="btn btn-sm btn-warning add-cart" data-id="{$vo.product_id}">加入采购计划</a>
                                    </td>
                                </tr>
                            </volist>
                            </tbody>
                        </table>
                    </div>
                    <div class="box-footer clearfix">
                        <input type="checkbox" class="grid-select-all">&nbsp;&nbsp;&nbsp;
                        <!--<a class="btn btn-sm btn-info batch-delete">保存</a>-->
                        <a class="btn btn-sm btn-primary grid-refresh"><i class="fa fa-refresh"></i></a>
                        {$page}
                    </div>
                    <!-- /.box-body -->
                </div>
            </div>

        </div>
    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>
<script type="text/javascript">
	showBigPicture();
</script>
<script data-exec-on-popstate>
    $(function (){
        $('.grid-select-all').change(function () {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });
        
        //加入采购蓝
        $('.add-cart').click(function () {
			var parent = $(this).parents(".validation-area");
			var gnobj = parent.find(".grid-num");
            var gnval = gnobj.val().trim();
			var gnmin = gnobj.attr("data-min");
			var num = gnval;
			var flag = false;
			if(mbValidator.isEmpty(gnval)){
				alert("采购数量不能为空！");	
			}else if(isNaN(gnval) || !mbValidator.isPositiveInt(gnval) || parseInt(gnval) < parseInt(gnmin)){
				alert("采购数量必须为大于等于"+gnmin+"整数！");
			}else{
				flag = true;	
			}
            if(!flag) return;
            var product_id = $(this).attr('data-id');
            
			
            if (product_id !='' && num != '') {
                $.ajax({
                    type: "POST",
                    url: "{:U('purchase/addToCart')}",
                    data: {product_id:product_id, num:num},
                    success: function(msg){
                        if (msg.status) {
                            alert(msg.message);
                        } else {
                            alert(msg.message)
                        }
                    },
                    error: function (msg) {
                        alert(msg.message);
                    }
                });
            }
        });

        /*$('.batch-delete').on('click',function(e){

            if(!confirm("是否继续")){
                return false;
            }
            var data = [];
            $(".grid-item").each(function (i) {
                if ($(this)[0].checked) {
                    var is_show = true;
                } else {
                    var is_show = false;
                }
                var product_id = $(this).parent().parent().find('td:eq(1)').text();
                var goods_id = $(this).parent().parent().find('td:eq(2)').text();
                var store = $(this).parent().parent().find('input[name=store]').val();
                var price = $(this).parent().parent().find('input[name=price]').val();

                data.push({product_id:product_id,goods_id:goods_id,store:store,price:price,is_show:is_show});
            });
            if (data) {
                $.ajax({
                    type: "POST",
                    url: "{:U('purchase/save_store_setup',array('local_id'=>$local_id))}",
                    data: {data:data},
                    success: function(msg){
                        if (msg.status) {
                            alert(msg.message);
                        } else {
                            alert(msg.message)
                        }
                        //window.location.reload();
                    },
                    error: function (msg) {
                        alert(msg.message);
                    }
                });
            }
        })*/

    });
</script>